package com.xc.stock.dto.model;

import org.apache.commons.lang3.StringUtils;

import com.ag.utils.Jsons;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.xc.stock.constant.RoleEnum;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 
 * title: OperInfo.java 
 * 操作信息
 * 
 * @author rplees
 * @email rplees.i.ly@gmail.com
 * @version 1.0  
 * @created Dec 7, 2020 4:15:56 PM
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DestInfo {
	RoleEnum re;
	String reText;
	
	String id;
	String name;
	String phone;
	
	public Integer idInt() {
		return Integer.parseInt(id);
	}
	
	public String operator() {
		return Joiner.on("-").skipNulls().join(re.code(), id, name, phone);
	}
	
	public OperInfo operInfo() {
		return OperInfo.builder()
				.re(re)
				.reText(re.remark())
				.operatorId(id)
				.operatorName(name)
				.operatorPhone(phone)
				.build();
	}
	
	JSONObject param;
	
	public boolean full() {
		return StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(name);
	}
	
	public void setRe(RoleEnum re) {
		this.re = re;
		this.reText = re.remark();
	}
	
	public JSONObject toJson() {
		JSONObject j = new JSONObject();
		j.put("id", id);
		if (name != null) {
			j.put("name", name);
		}
		if (phone != null) {
			j.put("phone", phone);
		}
		
		if(re != null) {
			j.put("re", re.code());
		}
		
		if (reText != null) {
			j.put("reText", reText);
		}
		
		if (param != null) {
			j.put("param", param);
		}
		return j;
	}
	
	public DestInfo putParam(String key, Object v) {
		if(param == null) {
			param = new JSONObject();
		}
		
		param.put(key, v);
		return this;
	}
	
	public <T> T getParam(String key, T d) {
		return Jsons.getByPath(param, key, d);
	}

    @Override
    public String toString() {
        return toJson().toJSONString();
    }
    
    public static DestInfo sys() {
    	return OperInfo.sys().destInfo();
    }
    
    public static DestInfo re(RoleEnum re) {
    	return DestInfo.builder()
    			.re(re)
    			.reText(re.remark())
    			.build();
    }
}
